<?php
require './model/Db.php';
$web_title = '激活账号';
$msg = '哦噢~账号激活失败了(token验证失败)';
$reurl = '';
if(isset($_GET['token'])){
	$Db = Db::getInstance();
	$token = $Db->escape($_GET['token']);
	$data1 = $Db->find([
		table => ['tmp_user'],
		where => "`token` = '{$token}'"
	]);
	if($data1){
		$data2 = $Db->find([
			table => ['tmp_user'],
			where => "`token` = '{$token}' and `active_time` > now()"
		]);
		

		if(!$data2){
			$msg = '哦噢~账号激活失败了(验证已经过期),请<a href="/register.php" style="color:inherit">重新注册</a>吧';
			$reurl = 'register.php';
		}else{
		   
			$isSaved = $Db->save([
				table => 'bbs_user',
				map => [
					user  => $data2[0]['user'],
					nick  => $data2[0]['nick'],
					pwd   => $data2[0]['pwd'],
					email => $data2[0]['email'],
					sex   => $data2[0]['sex'],
					phone => $data2[0]['phone'],
					photo => "default_{$data2[0]['sex']}.png",
					last_time => time(),
					my_follow => '{"uids":[],"mids":[],"cids":[]}',
					follow_me => '{"uids":[],"mids":[],"zids":[]}'
				]
			]);
	
			if($isSaved){
				$Db->remove([
					table => 'tmp_user',
					where => "`user` = '{$data2[0]['user']}' or `email` = '{$data2[0]['email']}'",
					flag  => true
				]);
				$msg = '账号激活成功了,快去<a href="login.php" style="color:inherit">登录</a>吧!';
				$img_type = 'success';
				$reurl = 'login.php';
			}
			
		}
	}
	$Db->close();
}

include './view/skip.html';
